﻿using HBKY.Shop.Common;
using HBKY.Shop.Common.Extension;
using HBKY.Shop.Model;
using HBKY.Shop.Model.Enum;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System.Linq;

namespace ZQCY.Watchmen.Extensions.Filter
{
    /// <summary>
    /// 参数校验Filter
    /// </summary>
    public class CustomResultFilter : ActionFilterAttribute
    {
        public override void OnResultExecuting(ResultExecutingContext context)
        {
            if (!context.ModelState.IsValid)
            {  
                ResponseMessage<string> messageModel = new ResponseMessage<string>();
                var result = context.ModelState.Keys
                        .SelectMany(key => context.ModelState[key].Errors.Select(x => x.ErrorMessage))
                        .ToList();
                messageModel.Data = MessageModel.ValidationError;
                messageModel.Code = StatusCodeEnum.Status1102ArgumentError.Key();
                messageModel.Message = string.Join("|", result);
                context.Result = new ObjectResult(messageModel);
            }
            base.OnResultExecuting(context);
        }
    }
}
